package cn.tqfeiyang.smartadmin.system.controller;

import cn.tqfeiyang.smartadmin.commons.pagination.QueryParams;
import cn.tqfeiyang.smartadmin.system.service.PermissionService;
import cn.tqfeiyang.smartadmin.system.request.PermissionCreateDTO;
import cn.tqfeiyang.smartadmin.system.request.PermissionUpdateDTO;
import cn.tqfeiyang.smartadmin.system.request.PermissionQueryDTO;
import cn.tqfeiyang.smartadmin.system.response.PermissionQueryVO;
import cn.tqfeiyang.smartadmin.system.response.PermissionDetailVO;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import cn.dev33.satoken.annotation.SaCheckPermission;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import cn.tqfeiyang.smartadmin.commons.pagination.PageData;
import cn.tqfeiyang.smartadmin.commons.response.ApiResult;
import cn.tqfeiyang.smartadmin.commons.validation.EntityId;
import cn.tqfeiyang.smartadmin.commons.logging.OperationLog;

import java.util.Set;

/**
 * 权限控制器
 *
 * @author tqfeiyang
 * @since 2024-08-23
 */
@RestController
@Validated
@RequestMapping("/api/system/permission")
@Tag(name = "系统/权限")
public class PermissionController {

    /**
     * 权限服务
     */
    @Resource
    private PermissionService permissionService;

    /**
     * 查询权限
     *
     * @param dto 请求数据
     * @return 权限列表
     */
    @OperationLog
    @GetMapping("/query")
    @Operation(summary = "查询权限")
    @SaCheckPermission("system:permission:query")
    public ApiResult<PageData<PermissionQueryVO>> queryPermissions(@Validated PermissionQueryDTO dto) {
        return ApiResult.success(permissionService.queryPermissions(dto));
    }

    /**
     * 获取权限
     *
     * @param permissionId 权限id
     * @return 权限信息
     */
    @OperationLog
    @GetMapping("/{permissionId}/read")
    @Operation(summary = "获取权限")
    @SaCheckPermission("system:permission:read")
    public ApiResult<PermissionDetailVO> readPermission(@PathVariable @EntityId Long permissionId) {
        return ApiResult.success(permissionService.readPermission(permissionId));
    }

    /**
     * 添加权限
     *
     * @param dto 请求数据
     * @return 权限信息
     */
    @OperationLog
    @PostMapping("/create")
    @Operation(summary = "添加权限")
    @SaCheckPermission("system:permission:create")
    public ApiResult<PermissionDetailVO> createPermission(@RequestBody @Validated PermissionCreateDTO dto) {
        Long permissionId = permissionService.createPermission(dto);
        return ApiResult.success(permissionService.readPermission(permissionId));
    }

    /**
     * 修改权限
     *
     * @param permissionId 权限id
     * @param dto 请求数据
     * @return 权限信息
     */
    @OperationLog
    @PostMapping("/{permissionId}/update")
    @Operation(summary = "修改权限")
    @SaCheckPermission("system:permission:update")
    public ApiResult<PermissionDetailVO> updatePermission(@PathVariable @EntityId Long permissionId, @RequestBody @Validated PermissionUpdateDTO dto) {
        permissionService.updatePermission(permissionId, dto);
        return ApiResult.success(permissionService.readPermission(permissionId));
    }

    /**
     * 删除权限
     *
     * @param permissionId 权限id
     */
    @OperationLog
    @PostMapping("/{permissionId}/delete")
    @Operation(summary = "删除权限")
    @SaCheckPermission("system:permission:delete")
    public ApiResult<String> deletePermission(@PathVariable @EntityId Long permissionId) {
        permissionService.deletePermission(permissionId);
        return ApiResult.success();
    }

    /**
     * 查询角色
     *
     * @param permissionId 权限id
     */
    @OperationLog
    @GetMapping("/{permissionId}/roles/query")
    @Operation(summary = "查询角色")
    @SaCheckPermission("system:permission:roles:query")
    public ApiResult<PageData<PermissionQueryVO>> queryRoles(@PathVariable @EntityId Long permissionId, @Validated QueryParams dto) {
        return ApiResult.success(permissionService.queryRoles(permissionId, dto));
    }

    /**
     * 修改角色
     *
     * @param permissionId 权限id
     * @param roleIds 角色id集合
     */
    @OperationLog
    @PostMapping("/{permissionId}/roles/update")
    @Operation(summary = "修改角色")
    @SaCheckPermission("system:permission:roles:update")
    public ApiResult<String> updateRoles(@PathVariable @EntityId Long permissionId, @RequestBody Set<Long> roleIds) {
        permissionService.updateRoles(permissionId, roleIds);
        return ApiResult.success();
    }
}
